<?php

namespace app\admin\validate;

use think\Validate;
use think\Db;
class App extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'name' => 'checkName:thinkphp',
    ];
    /**
     * 提示消息
     */
    protected $message = [

    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    /**
     * 自定义验证规则,同类型下，应用名称不能相同
     */
    protected function checkName($value,$rule,$data=[])
    {
        $id=(substr($rule,strpos($rule,',')+1));
        $list=Db::table('t_app')
            ->alias('a')
            ->where([['a.name','=',$value],["a.id",'neq',$id]])
            ->select();
        if($list==[]){
            return true;
        }
        foreach ($list as $v){
            if($v['app_type_id']==$data['app_type_id']){
                return "同一应用类型下，应用名称不能相同";
            }
        }
        return true;
    }
    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'name'  => __('Name'),
        ];
        //parent::__construct($rules, $message, $field);
    }

}
